#ifndef __TCP_H__
#define __TCP_H__
#include "HPSocket.h"
extern CTcpServerPtr* m_pServer;
extern CTcpAgentPtr* m_pAgent;
#define 	m_Server (*m_pServer)
#define   	m_Agent  (*m_pAgent)

class TcpServerListener : public CTcpServerListener
{
public:
	TcpServerListener();
	~TcpServerListener();
	bool Strart(int port);
	EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient);
	virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
public:
};

struct TTcpContext
{
	volatile LONG	ref;
	volatile CONNID	serverConnID;
	volatile CONNID	agentConnID;

	BOOL		closed;

	static TTcpContext* Construct(CONNID svrConnID) { return new TTcpContext(svrConnID); }
	static void Destruct(TTcpContext* pObj) { delete pObj; }

	LONG IncRef() { return ::InterlockedIncrement(&ref); }

	LONG DecRef()
	{
		LONG val = ::InterlockedDecrement(&ref);

		if (val == 0)
			Destruct(this);

		return val;
	}

private:
	TTcpContext(CONNID svrConnID)
		: ref(1L)
		, serverConnID(svrConnID)
		, agentConnID(0)
		, closed(FALSE)
	{

	}

};
#endif // !__TCP_H__

